package com.leo.util;

import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DecryptUtil2 {

	private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

	public static String initkey() {
		String[] keys = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
				"y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
		StringBuffer kstr = new StringBuffer();
		for (int i = 0; i < 8; i++) {
			int d = new Random().nextInt(62);
			kstr.append(keys[d]);
		}
		return kstr.toString();
	}

	public static String encryptDES(String encryptString, String encryptKey) throws Exception {
		// IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		IvParameterSpec zeroIv = new IvParameterSpec(iv);
		SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

		return Base64.encode(encryptedData);
	}

	public static String decryptDES(String decryptString, String decryptKey) throws Exception {
		byte[] byteMi = Base64.decode(decryptString);
		IvParameterSpec zeroIv = new IvParameterSpec(iv);
		// IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);
		SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
		byte decryptedData[] = cipher.doFinal(byteMi);

		return new String(decryptedData);
	}

	public static String encryptDES(String encryptString) throws Exception {
		String encryptKey = initkey();
		return encryptKey.concat(encryptDES(encryptString, encryptKey));
	}

	/**
	 * 协议解析串
	 */
	public static String decryptDES(String src) throws Exception {
		String urlKey = src.substring(0, 8);
		String readlyContent = src.substring(8);
		return decryptDES(readlyContent, urlKey);
	}

	public static void main(String[] args) {
		String dd = "hCHWPB9LtVttR/Pg2TzVLVNqTVcs+9UgN4yCY/xQLfHoMQD1yoDJyzgOOCpnua6WJFe3 jRi6AqQMMN+fFZwk1cb39Om+80HfysAiAq9PtVVAYjt2y7rMOB65UExec7eG 8ehuVwQYfgbVVw1f5lQ8+tkyiqQ2Hii134L/RcUWc6F367m/kJ+Y0Dez+H94 zww7HyAe8zxXsim3Pn57PRGEQAKABtcX+7oJiYC62JUEaZiN+DtxRA27Ainb 56UOrMqG8PUD6577mrgqdoAENnOLeLisCkbp6Oy3G3eKmDgigxNZOD8AtTOx lLh/GeintPlWd835XBazB3D4KyeZBigQWU9QQfHkC/wCN34ZZsBP3WvWi3TH tn7eotMmrTDisdAoYUyqx6dekkSnOM5wVh3ywQoPZw0RUK5RMbAOCYDokrp3 Cw+HCPY5bULhubWWEaOZfkDs7PulzthWhs4N2QqowS+IVSDGums6g79cOY0R 42euIr0SbGHSp4x8RzDq3dTRpqUl1qxWjHcbXZRFDNlhe4c1IQwL3rhiXbWO KFJN";
		dd = "{\"a\":\"\",\"bd\":\"BLU\",\"bi\":3,\"c\":\"S0014001\",\"d\":\"b452f791ff886e4d\",\"db\":\"\",\"e\":\"\",\"g\":0,\"ki\":22,\"l\":1,\"lc\":\"zh-CN\",\"m\":0,\"md\":\"DASH_X_PLUS_LTE\",\"o\":1,\"pn\":\"MT6735P\",\"qt\":\"com.kxw.applock,com.amazon.appstore.shortcut\",\"r\":\"3351.55\",\"s\":\"000000\",\"sd\":\"3301.55\",\"si\":\"3\",\"src\":\"720*1184\",\"sp\":0,\"sv\":\"5.1\",\"t\":1,\"test\":1,\"v\":\"3.1.4.606.0\",\"vc\":5,\"w\":1,\"x\":\"BLU_DASH_X_PLUS_LTE\",\"mc\":\"00:08:22:fc:70:fc\",\"gp\":0}";
		try {
			
			//System.out.println(decryptDES(dd));
			System.out.println(encryptDES(dd));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
